#include "user_login.h"

#define LOGIN "login"
#define REGISTER "register"
#define LOGOFF "logoff"

int user_input(user_t *user){
    bzero(user,sizeof(user_t));
    printf("Input username:");
    fgets(user->name,sizeof(user->name),stdin);
    //把末尾的\n替换为\0
    user->name[strcspn(user->name,"\n")] = '\0';
    //输入密码
    char *passwd = getpass("Input passwd:");
    strncpy(user->passwd,passwd,sizeof(user->passwd));
    user->passwd[sizeof(user->passwd) - 1] = '\0';
    return 0;
}

int user_send(const int sockfd,cmd_t *cmdInfo,user_t *user){
    memcpy(cmdInfo->args,user,sizeof(user_t));
    train_t train;
    bzero(&train,sizeof(train_t));
    train.length = sizeof(cmd_t);
    memcpy(train.data,cmdInfo,sizeof(cmd_t));

    user_t user2;
    memcpy(&user2,cmdInfo->args,sizeof(user_t));
    printf("train username = %s, passwd = %s\n", user2.name, user2.passwd);
    
    send_msg(sockfd,&train);

    /* bzero(&train,sizeof(train_t)); */
    /* train.length = sizeof(user_t); */
    /* memcpy(train.data, user, sizeof(user_t)); */
    /* printf("train username = %s, passwd = %s\n", user->name, user->passwd); */
    /* send_msg(sockfd,&train); */

    return 0;
}

int user_login(const int sockfd,cmd_t *cmdInfo,user_t *user){
    int login_flag = 1;
    while(login_flag == 1){
        /* printf("Please input: \n"); */
        printf("%s\n%s\n%s\n","1 login","2 register","3 logoff");
        printf("Please input: \n");
        fgets(cmdInfo->cmd, sizeof(cmdInfo->cmd), stdin); // 读取命令
        cmdInfo->cmd[strcspn(cmdInfo->cmd, "\n")] = '\0'; // 去掉换行符;

        if(strcmp(cmdInfo->cmd,"1") == 0){
            bzero(cmdInfo,sizeof(cmd_t));
            strcpy(cmdInfo->cmd,LOGIN);
        }else if(strcmp(cmdInfo->cmd,"2") == 0){
            bzero(cmdInfo,sizeof(cmd_t));
            strcpy(cmdInfo->cmd,REGISTER);
        }else if(strcmp(cmdInfo->cmd,"3") == 0){
            bzero(cmdInfo,sizeof(cmd_t));
            strcpy(cmdInfo->cmd,LOGOFF);
        }

        if(strcmp(cmdInfo->cmd,"login") == 0){
            //登录
            user_input(user);

            if(strcmp(user->name,"") == 0){
                printf("Name cannot be empty!\n");
                continue;
            }

            bzero(cmdInfo,sizeof(cmd_t));
            strcpy(cmdInfo->cmd,LOGIN);

            user_send(sockfd,cmdInfo,user);
            train_t train;
            bzero(&train,sizeof(train_t));

            char output[4096] = {0};//输出字符串用
            bzero(&output,sizeof(output));
            //接收返回的字符串
            int ret = recv_msg(sockfd,&train);
            if(ret == 0){
                close(sockfd);
                return 0;
            }
            memcpy(output,train.data,train.length);
            printf("%s",output);
            if(strcmp(output,"success")==0){
                login_flag = -1;
                char buf[20] = "success";
                bzero(&train,sizeof(train_t));
                train.length = sizeof(buf);
                memcpy(train.data, buf, sizeof(buf));
                send_msg(sockfd,&train);
            }
            //else{
            //    memcpy(output,train.data,train.length);
            //    printf("%s\n",output);
            //}
            if(login_flag == 1){
                printf("User name and password do not match!Please input again.\n");
            }

        }
        else if(strcmp(cmdInfo->cmd,"register") == 0){
            //注册
            user_input(user);

            if(strcmp(user->name,"") == 0){
                printf("Name cannot be empty!\n");
                continue;
            }

            bzero(cmdInfo,sizeof(cmd_t));
            strcpy(cmdInfo->cmd,REGISTER);

            user_send(sockfd,cmdInfo,user);
            train_t train;
            bzero(&train,sizeof(train_t));

            char output[4096] = {0};//输出字符串用
            bzero(&output,sizeof(output));
            //接收
            int ret = recv_msg(sockfd,&train);
            if(ret == 0){
                close(sockfd);
                return 0;
            }
            memcpy(output,train.data,train.length);
            printf("%s\n",output);
            //发送确认信息
            char buf[20] = {0};
            fgets(buf, sizeof(buf), stdin); // 读取命令
            buf[strcspn(buf, "\n")] = '\0'; // 去掉换行符;
            bzero(&train,sizeof(train_t));
            train.length = sizeof(buf);
            memcpy(train.data, buf, sizeof(buf));
            send_msg(sockfd,&train);

            //接收返回的字符串
            bzero(&train,sizeof(train_t));
            ret = recv_msg(sockfd,&train);
            if(ret == 0){
                close(sockfd);
                return 0;
            }
            bzero(&output,sizeof(output));
            memcpy(output,train.data,train.length);
            printf("%s\n",output);
            if(strcmp(output,"success")==0){
                printf("Register Success!\n");
                continue;
            }
            else{
                memcpy(output,train.data,train.length);
                printf("%s\n",output);
            }
        }
        else if(strcmp(cmdInfo->cmd,"logoff") == 0){
            //注销
            user_input(user);

            if(strcmp(user->name,"") == 0){
                printf("Name cannot be empty!\n");
                continue;
            }

            bzero(cmdInfo,sizeof(cmd_t));
            strcpy(cmdInfo->cmd,LOGOFF);

            user_send(sockfd,cmdInfo,user);
            train_t train;
            bzero(&train,sizeof(train_t));

            char output[4096] = {0};//输出字符串用
            bzero(&output,sizeof(output));

            //接收确认信息
            int ret = recv_msg(sockfd,&train);
            if(ret == 0){
                close(sockfd);
                return 0;
            }
            memcpy(output,train.data,train.length);
            printf("%s\n",output);
            //发送确认信息
            char buf[20] = {0};
            fgets(buf, sizeof(buf), stdin); // 读取命令
            buf[strcspn(buf, "\n")] = '\0'; // 去掉换行符;
            bzero(&train,sizeof(train_t));
            train.length = sizeof(buf);
            memcpy(train.data, buf, sizeof(buf));
            send_msg(sockfd,&train);

            //接收返回的字符串
            bzero(&train,sizeof(train_t));
            ret = recv_msg(sockfd,&train);
            if(ret == 0){
                close(sockfd);
                return 0;
            }
            bzero(&output,sizeof(output));
            memcpy(output,train.data,train.length);
            printf("%s\n",output);
            if(strcmp(output,"success")==0){
                printf("Logoff Success!\n");
                continue;
            }
            else{
                memcpy(output,train.data,train.length);
                printf("%s\n",output);
            }
        }
        else{
            printf("CMD ERROR!\n");
            continue;
        }
    }


    return 0;
}

